package com.sanpu.kxe.business.main.presenter;

import android.app.Activity;
import android.text.TextUtils;

import com.sanpu.kxe.business.main.bean.UserCenterOutput;
import com.sanpu.kxe.business.main.contract.NickNameModifyContract;
import com.sanpu.kxe.business.main.model.MainModel;
import com.sanpu.kxe.common.cache.CacheManage;
import com.sanpu.kxe.common.cache.CacheModel;
import com.sanpu.kxe.jump.eventbus.RefreshNickNameEvent;
import com.sanpu.kxe.utils.AppContextUtil;
import com.sanpu.kxe.utils.ToastUtil;
import com.sanpu.kxe.utils.VerifyUtil;

import org.greenrobot.eventbus.EventBus;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * Description：修改昵称Presenter
 * Created by 薛勇军 on 2018/7/11.
 */

public class NickNameModifyPresenter implements NickNameModifyContract.Presenter {

    private NickNameModifyContract.View mView;

    private CompositeSubscription mSubscription;

    public NickNameModifyPresenter(NickNameModifyContract.View mView) {
        this.mView = mView;
        mSubscription = new CompositeSubscription();
    }

    @Override
    public void onCommit(final String nickName, String reName) {
        if (TextUtils.isEmpty(nickName)) {
            ToastUtil.showTextViewPrompt("昵称不能为空！");
            return;
        }
        if (TextUtils.equals(nickName, reName)) {
            ((Activity) mView.getContext()).finish();
            return;
        }
        if (!VerifyUtil.checkName(nickName)) {
            ToastUtil.showTextViewPrompt("姓名不允许输入特殊符号！");
            return;
        }
        mView.showLoadingDialog(false);
        Subscription subscription = MainModel.getInstance().updateNickName(nickName)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onCompleted() {
                        mView.dismissLoadingDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoadingDialog();
                        mView.showErrorText(e);
                    }

                    @Override
                    public void onNext(String s) {
                        UserCenterOutput output = CacheManage.getInstance().getCache(CacheModel.USER_CENTER_KEY);
                        if (output != null) {
                            output.setNickName(nickName);
                            CacheManage.getInstance().saveCache(CacheModel.USER_CENTER_KEY, output);
                            ToastUtil.showTextViewPrompt("修改昵称成功！");
                            EventBus.getDefault().post(new RefreshNickNameEvent());
                            ((Activity) mView.getContext()).finish();
                        } else {
                            ToastUtil.showTextViewPrompt("登录过期，请重新登录！");
                            AppContextUtil.exit(mView.getContext());
                        }
                    }
                });
        mSubscription.add(subscription);
    }

    @Override
    public void onDestroyPresenter() {
        if (mView != null)
            mView = null;
        if (mSubscription != null && !mSubscription.isUnsubscribed()) {
            mSubscription.unsubscribe();
            mSubscription = null;
        }
    }
}
